package com.hzdl.apple.controller.backCotroll;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSON;
import com.hzdl.apple.bean.Admin;
import com.hzdl.apple.bean.User;
import com.hzdl.apple.service.back.AdminService;
import com.hzdl.apple.service.back.BackUserService;
import com.hzdl.apple.utils.DruidUtils;
import com.hzdl.apple.utils.HttpUtils;

import net.sf.json.JSONObject;


@WebServlet("/manager/*")
public class manager extends HttpServlet{
	private AdminService adminService;
	private BackUserService userService;
	private Connection conn;
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		// 把项目路径存储到application中
		adminService = new AdminService();
		userService = new BackUserService();
		try {
			conn = DruidUtils.dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		getServletContext().setAttribute("path", getServletContext().getContextPath());
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		String baseUri = req.getRequestURI();
		JSONObject json = new JSONObject();
		if (baseUri.contains("/tologin")) {// 后台首页
			HttpUtils.forward("back/back-login.jsp", req, resp);
		}else if(baseUri.contains("/login")){
			String ALoginId = req.getParameter("username");
			String APsw = req.getParameter("password");
			Admin admin =adminService.AdminLogin(new Admin(ALoginId, APsw), conn) ;
			if(admin == null){
				HttpUtils.forward("back/back-login.jsp", req, resp);
			}else{
				req.getSession().setAttribute("Admin", admin);
				HttpUtils.forward("back/back-index.jsp", req, resp);
			}
		}else if(baseUri.contains("/toUserEdit")){
			List<User> userList = userService.getAllUser();
			req.setAttribute("data", JSON.toJSONString(userList));
			HttpUtils.forward("back/back-UserEdit.jsp", req, resp);
		}else if(baseUri.contains("/toUserEditInit")){
			List<User> userList = userService.getAllUser();
			req.setAttribute("data", JSON.toJSONString(userList));
		}else if(baseUri.contains("/AddUser")){
			String uName = req.getParameter("uName");
			String uLoginId = req.getParameter("uLoginId");
			String uLoginPsw = req.getParameter("uLoginPsw");
			String uAddress = req.getParameter("uAddress");
			String uPhoneNum = req.getParameter("uPhoneNum");
			User user = new User(uName, uLoginId, uLoginPsw, uAddress, uPhoneNum);
			boolean b = userService.addUser(user);
			if(b){
			json.put("success", "true");
			resp.getWriter().write(json.toString());
			}else{
				json.put("error", "false");
				resp.getWriter().write(json.toString());
			}
		}else if(baseUri.contains("/UpdateUser")){
			String uID = req.getParameter("uID");
			String uName = req.getParameter("uName");
			String uLoginId = req.getParameter("uLoginId");
			String uLoginPsw = req.getParameter("uLoginPsw");
			String uAddress = req.getParameter("uAddress");
			String uPhoneNum = req.getParameter("uPhoneNum");
			User user = new User(uName, uLoginId, uLoginPsw, uAddress, uPhoneNum);
			boolean b = userService.updateUser(uID, user);
			if(b){
				json.put("success", "true");
				resp.getWriter().write(json.toString());
			}else{
				json.put("error", "false");
				resp.getWriter().write(json.toString());
			}
			List<User> userList = userService.getAllUser();
			req.setAttribute("data", JSON.toJSONString(userList));
			HttpUtils.forward("back/back-UserEdit.jsp", req, resp);
		}else if(baseUri.contains("/UpdateUserUI")){
			String uID = req.getParameter("uID");
			User user = userService.getUserByID(uID);
			req.setAttribute("user", user);
			HttpUtils.forward("back/back-UserEdit.jsp", req, resp);
		}else if(baseUri.contains("/DeleteUser")){
			String uID = req.getParameter("uID");
			boolean b = userService.deleteUser(uID);
			if(b){
				json.put("success", "true");
				resp.getWriter().write(json.toString());
			}else{
				json.put("error", "false");
				resp.getWriter().write(json.toString());
			}
			HttpUtils.forward("back/back-UserEdit.jsp", req, resp);
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}
